﻿using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using LogSystem;
using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Gameplay.Liberty;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")][Description("一个Npc看向另一个Npc")]
    public class NpcLookAt : LatentActionNodeBase
    {
        private float _duration;
        private float _totalTime;
        
        //[HideInInspector]
        //public FlowOutput onUpdate;
        
        //public override bool allowRoutineQueueing { get { return false; } }

        public IEnumerator Invoke(int npcId,int otherNpcId,float duration)
        {
            _duration = duration;
            _totalTime = duration;
            
            GameObject npc = BlueprintUtils.GetNpc(npcId);
            GameObject otherNpc = BlueprintUtils.GetNpc(otherNpcId);

            if (npc && otherNpc)
            {
                Vector3 pos1 = npc.transform.position;
                Vector3 pos2 = otherNpc.transform.position;
                
                if (duration < 0.001f)
                {
                    npc.transform.LookAt(new Vector3(pos2.x,pos1.y,pos2.z), Vector3.up);
                    yield break;
                }
                else
                {
                    Quaternion lookQuaternion =
                        Quaternion.LookRotation(new Vector3(pos2.x,pos1.y,pos2.z)  - new Vector3(pos1.x,pos1.y,pos1.z));
                    Quaternion origin = npc.transform.rotation;
                    
                    while (true)
                    {
                        _duration -= Time.deltaTime;
                        float t = 1 - (_duration / _totalTime);
                        t = Mathf.Max(0, t);
                        npc.transform.rotation =
                            Quaternion.Slerp(origin, lookQuaternion,t);
                    
                        if (_duration <= 0) {
                            yield break;
                        }
                        else {
                            yield return null;
                        }
                    }
                }
            }
            else
            {
                if (npc == null)
                {
                    Debug.LogError(LogModule.Blueprint,"找不到此Npc：" + npcId);
                }

                if (otherNpc == null)
                {
                    Debug.LogError(LogModule.Blueprint,"找不到此Npc：" + otherNpcId);
                }
                yield break;
            }


        }
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            var p1 = node.AddValueInput<int>(parameters[0].Name);
            var p2 = node.AddValueInput<int>(parameters[1].Name);
            var p3 = node.AddValueInput<float>(parameters[2].Name);
            
            //onUpdate = node.AddFlowOutput("onUpdate");

            node.AddFlowInput("In", (f) => { Begin(Invoke(p1.value,p2.value,p3.value), f); });

        }
    }
}